package com.matchmaking.system.controller;


import cn.hutool.core.map.MapUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.matchmaking.system.config.R;
import com.matchmaking.system.domain.SysDept;
import com.matchmaking.system.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/sysDept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    /***
     *部门列表查询方法
     * @param sysDept
     * @return
     */
    @PostMapping("/list")
    public R listAll(@RequestBody SysDept sysDept) {
        List<SysDept> list = sysDeptService.listAll(sysDept);
        return R.successed(list);
    }

    /***
     * 部门逻辑删除
     * @param id
     * @return
     */
    @PostMapping("/remove")
    public R remove (Integer id){
        SysDept sysdept = sysDeptService.getById(id);
        sysdept.setDelFlag("1");
        sysDeptService.removeId(sysdept);
        return R.successed(null);
    }

    /***
     * 部门数据添加
     * @param sysDept
     * @return
     */
    @PostMapping("/save")
    public R saveDept (@RequestBody SysDept sysDept){
        sysDeptService.saveDept(sysDept);
        return R.successed(null);
    }

    /***
     * 部门数据修改
     * @param sysDept
     * @return
     */
    @PostMapping("/update")
    public R updateDept(@RequestBody SysDept sysDept){
        System.err.println(sysDept);
        sysDeptService.updateDept(sysDept);
        return R.successed(null);
    }

    /***
     * 部门数据修改回显
     * @param id
     * @return
     */
    @PostMapping("/updateSysDept")
    public SysDept updateSysDept(Integer id){
        SysDept sysDept = sysDeptService.updateSysDept(Long.valueOf(id));
        return sysDept;
    }

    /***
     * 部门数据报表导出
     * @param sysDept
     * @param response
     */
    @PostMapping("/export")
    public void export(@RequestBody SysDept sysDept, HttpServletResponse response){
        List<SysDept> list = sysDeptService.selectExport(sysDept);
        System.err.println(list);
        sysDeptService.exprot(list,response);
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        try {
            List<SysDept> list = sysDeptService.download();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheeet");
            response.setCharacterEncoding("utf-8");
            //防止中文乱码
            String fileName = URLEncoder.encode("部门", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition","attachment;filename*=utf-8''" + fileName + ".xlsx");
            //这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(),SysDept.class)
                    .autoCloseStream(Boolean.FALSE)
                    .sheet()
                    .doWrite(list);


        } catch (Exception e){

            //重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            HashMap<String, String> map = MapUtil.newHashMap();
            map.put("status","failure");
            map.put("message","下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));

        }
    }

    /***
     * 根据负责人查询部门数量
     * @return
     */
    @PostMapping("/countList")
    public R countList(){
        List<SysDept> list = sysDeptService.listCount();
        return R.successed(list);
    }

    /***
     * 查询出创建部门数量最多的创建者
     * @return
     */
    @PostMapping("/countListb")
    public R countListb(){
        List<SysDept> list = sysDeptService.listCountB();
        return R.successed(list);
    }

    /***
     * 统计每个父部门id下的部门数量：
     * @return
     */
    @PostMapping("/countListC")
    public R countListC(){
        List<SysDept> list = sysDeptService.listCountC();
        return R.successed(list);
    }

    @PostMapping("/countListd")
    public R countListD(){
        List<SysDept> list = sysDeptService.listCountD();
        return R.successed(list);
    }



}






